﻿using distpm.Attibute;
using Nancy;
using System;
using System.Collections.Generic;
using distpm.Model;
using distpm.Model.ViewModel;
namespace distpm.module
{
    public class HomeModule:NancyModule
    {   
        public HomeModule()
        {
            Get["/Article/{id}"] = _ => _.id;
            Get["/Articles/page={pageid}"] = arg =>
            {
                var pageid = arg.pageid;
                List<Article> articles = new List<Article>();
                
                for (int i = 1; i <= 10; i++)
                {
                    Article article = new Article();
                    article.A_Title = "序号:"+i+DateTime.Now.ToLongTimeString();
                    article.A_PublishDatet = DateTime.Now.Date.ToShortTimeString();
                    article.A_Content = "ABCDEFGHIJKLMNOPQRSTUVWXY";
                    articles.Add(article);
                }
                return Response.AsJson(new JsonModel { Data = articles, Msg = "正常", ResponseResult = ResponseResult.Ok });
            };
            Get["/"] = arg =>
            {
               
                if (CheckToken(this.Request))
                {
                    return Response.AsText("登录成功");
                }
                return Response.AsJson(new JsonModel{Data =null,Msg="登陆失败",ResponseResult =ResponseResult.Error}, HttpStatusCode.BadRequest);

            };
        }

        public bool CheckToken(Request r)
        {
            string token = string.Empty;
            foreach (var variable in r.Headers)
            {
                if (variable.Key == "auth")
                {
                    token = variable.Value.ToString();
                }

            }
            if (string.IsNullOrEmpty(token))
            {
                return false;
            }
            return true;

        }

    }
   
}